package com.molichuxing.services.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.business.dto.request.create.AdvertBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.AdvertBizModifyDto;
import com.molichuxing.services.business.dto.response.AdvertBizDto;
import com.molichuxing.services.business.service.AdvertBizService;
import com.molichuxing.services.infrastructure.dto.request.create.AdvertCreateDto;
import com.molichuxing.services.infrastructure.dto.request.create.AdvertisingInfoCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.AdvertisingInfoModifyDto;
import com.molichuxing.services.infrastructure.dto.response.AdvertDto;
import com.molichuxing.services.infrastructure.dto.response.AdvertisingInfoDto;
import com.molichuxing.services.infrastructure.service.AdvertService;
import com.molichuxing.services.infrastructure.service.AdvertisingInfoService;
import com.molichuxing.services.infrastructure.service.AdvertisingStoreService;
import com.molichuxing.services.property.AdvertisingInfoMediaEnum;

@Service("advertBizService")
public class AdvertBizServiceImpl implements AdvertBizService{

	@Autowired
	private AdvertService advertService;
	
	@Autowired
	private AdvertisingInfoService advertisingInfoService;
	
	@Autowired
	private AdvertisingStoreService advertisingStoreService;
	
	/**
	 * 获取广告列表
	 * 
	 * @param map
	 *            参数
	 *key:name
	 *            String 广告名称 非必填
	 *key:position
	 *            Integer 广告位置（1:终端屏保广告   2:APP开机广告） 非必填
	 *key:startTime
	 *            String 请求开始时间 非必填
	 *key:endTime
	 *            String 请求结束时间 非必填
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<AdvertBizDto> getAdvertList(Map<String, Object> map) {
		
		Paged<AdvertDto> paged = advertService.getAdvertList(map);
		if(null == paged) {
		    return null;
		}else if(paged.isEmpty()) {
		    return  new Paged<>(null,paged.getTotal(),paged.getPageSize());
		}
		List<AdvertDto> list = paged.getList();
		List<AdvertBizDto> dtoList =new ArrayList<>();
		for (AdvertDto dto : list) {
			AdvertBizDto bizDto = new AdvertBizDto();
			BeanUtils.copyProperties(dto,bizDto);
			Integer storeCount= advertisingStoreService.getStoreCountByAdvertId(dto.getId());
			bizDto.setStoreCount(storeCount);
			dtoList.add(bizDto);
		}
		Long total = paged.getTotal();
		Integer pages = paged.getPages();
		Paged<AdvertBizDto> page = new Paged<>(dtoList,total,pages);
		return page;
	}
	
	/**
	 * 
	 * 根据广告id获取广告信息
	 * @param id
	 * @return
	 */
	@Override
	public AdvertBizDto getAdvertById(Integer id) {
		AdvertDto dto = advertService.getAdvertById(id);
		if(null == dto) {
			return null;
		}
		Integer advertId = dto.getId();
		AdvertBizDto bizDto = new AdvertBizDto();
		BeanUtils.copyProperties(dto,bizDto);
		//获取图片资源
		bizDto.setImgList(getAdvertisingInfoDtoByAdvertId(advertId,AdvertisingInfoMediaEnum.IMG.getValue()));
		//获取视频资源
		bizDto.setVideoList(getAdvertisingInfoDtoByAdvertId(advertId,AdvertisingInfoMediaEnum.VIDEO.getValue()));
		return bizDto;
	}
	
	/**
	 * 获取广告素材
	 * @param advertId
	 * @param media
	 * @return
	 */
	private List<String> getAdvertisingInfoDtoByAdvertId(Integer advertId,String media) {
		List<AdvertisingInfoDto> list = advertisingInfoService.getAdvertisingInfoDtoByAdvertId(advertId, media);
		if(null == list || list.isEmpty()) {
			return null;
		}
		List<String> imgList =new ArrayList<>(list.size());
		for (AdvertisingInfoDto dto : list) {
			imgList.add(dto.getUrl());
		}
		return imgList;
	}
	
	/**
	 * 新增广告
	 * @param createDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5000, rollbackFor = Exception.class)
	public Boolean create(AdvertBizCreateDto createDto) {
		AdvertCreateDto advertDto = new AdvertCreateDto();
		BeanUtils.copyProperties(createDto, advertDto);
		Integer advertId = advertService.create(advertDto);
		if(null == advertId) {
			throw new BizException(101, "添加广告失败");
		}
		List<String> imgList = createDto.getImgList();
		if(null != imgList && !imgList.isEmpty()) {
			for(int i=0; i<imgList.size() ;i++) {
				int sequence =i+1;
				create(sequence,advertId,imgList.get(i),AdvertisingInfoMediaEnum.IMG.getValue());
			}
		}
		List<String> videoList = createDto.getVideoList();
		if(null != videoList && !videoList.isEmpty()) {
			for(int i=0; i<videoList.size() ;i++) {
				int sequence =i+1;
				create(sequence,advertId,videoList.get(i),AdvertisingInfoMediaEnum.VIDEO.getValue());
			}
		}
		return true;
	}
	
	/**
	 * 保存素材接口
	 * @param sequence
	 * @param advertId
	 * @param url
	 * @param media
	 * @return
	 */
	private Boolean create(Integer sequence,Integer advertId,String url,String media) {
		AdvertisingInfoCreateDto createDto = new AdvertisingInfoCreateDto();
		createDto.setSequence(sequence);
		createDto.setAdvertId(advertId);
		createDto.setUrl(url);
		createDto.setIsDelete((short)0);
		createDto.setForceUpdate((short)1);
		createDto.setMd5Value("md5");
		createDto.setMedia(media);
		return advertisingInfoService.create(createDto);
	}
	
	
	/**
     * 修改广告
     * @param modifyDto
     * @return
     */
	@Override
	public Boolean modify(AdvertBizModifyDto modifyDto) {
		AdvertDto bean = advertService.getAdvertById(modifyDto.getAdvertId());
		if(null == bean) {
			throw new BizException(101, "不存在该条广告信息");
		}
		AdvertisingInfoModifyDto advertisingDto = new AdvertisingInfoModifyDto();
		BeanUtils.copyProperties(modifyDto, advertisingDto);
		return advertisingInfoService.modify(advertisingDto);
	}
	
	 /**
     * 新增门店和广告的绑定关系
     * @param storeIds
     * @param advertId
     * @return
     */
	@Override
	public Boolean createAdvertStore(List<Integer> storeIds, Integer advertId) {
		AdvertDto bean = advertService.getAdvertById(advertId);
		if(null == bean) {
			throw new BizException(101, "不存在该条广告信息");
		}
		return advertisingStoreService.createAdvertStore(storeIds, advertId);
	}
	
}
